package com.zzj.aop;

import com.zzj.entity.Post;
import com.zzj.model.dto.CommentCreateDto;
import com.zzj.model.dto.comment.ReplyCreateDto;
import com.zzj.service.IPostService;
import jakarta.annotation.Resource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CommentCountAop {


    @Resource
    private IPostService postService;

    @Pointcut("@annotation(com.zzj.annotation.CommentCountAnnotation)")
    public void commentCountAopPointcut() {
    }

    @After("commentCountAopPointcut()")
    public void incrementCommentCount(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        Object arg = args[0];
        if (arg instanceof CommentCreateDto) {
            postService.lambdaUpdate()
                    .eq(Post::getId, ((CommentCreateDto) arg).getTargetId())
                    .setSql("comments = comments + 1").update();
        }
    }

}
